<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <title>修改宠物</title>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
            aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="/admin/user_index.html">用户管理</a>
            </li>
            <li class="nav-item active">
                <a class="nav-link" href="/admin/pet_index.html">宠物管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/admin/adopt_index.html">领养管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/logout">退出</a>
            </li>
        </ul>
    </div>
</nav>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-12">
            <h2>修改宠物</h2>
            <form action="/admin/pet/update" method="post">
                <input type="hidden" id="id" name="id">
                <div class="form-group">
                    <label for="name">宠物名称:</label>
                    <input type="text" class="form-control" id="name" name="name" placeholder="请输入宠物名称">
                </div>
                <div class="form-group">
                    <label for="intro">宠物简介:</label>
                    <input type="text" class="form-control" id="intro" name="intro"
                           placeholder="请输入宠物简介">
                </div>

                <div class="form-group">
                    <label for="type">宠物类型:</label>
                    <select id="type" name="type" class="form-control">
                        <option value="狗狗">狗狗</option>
                        <option value="猫咪">猫咪</option>
                        <option value="兔子">兔子</option>
                        <option value="鸟类">鸟类</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="gender">宠物性别:</label>
                    <select id="gender" name="gender" class="form-control">
                        <option value="雄性">雄性</option>
                        <option value="雌性">雌性</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="status">领养状态:</label>
                    <select id="status" name="status" class="form-control">
                        <option value="未领养">未领养</option>
                        <option value="已领养">已领养</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="image1">宠物图片:</label>
                    <input type="file" class="form-control" id="image1" name="image1"
                           placeholder="请上传宠物图片">
                </div>

                <img id="imagePreview" src="#" style="max-width: 100%; max-height: 200px;" alt="预览">

                <input type="hidden" id="image" name="image"><br>

                <button type="submit" class="btn btn-primary">保存</button>
            </form>
        </div>
    </div>
</div>

<script src="/js/jquery.min.js"></script>
<script>
    $("#image1").change(function () {
        var formData = new FormData();
        formData.append("image1", $("#image1")[0].files[0]);
        $.ajax({
            url: "/upload",
            type: "POST",
            data: formData,
            processData: false,
            contentType: false,
            success: function (imagePath) {
                $("#image").val(imagePath);
                $("#imagePreview").attr("src", imagePath);
            },
            error: function (error) {
                console.log(error);
            }
        });
    });


    $(document).ready(function () {
        var petId = getParameterByName("id");
        $.ajax({
            url: "/admin/pet/get?id=" + petId,
            type: "GET",
            dataType: "json",
            success: function (data) {
                $("#id").val(data.id);
                $("#name").val(data.name);
                $("#intro").val(data.intro);
                $("#type").val(data.type);
                $("#gender").val(data.gender);
                $("#status").val(data.status);
                $("#image").val(data.image);
                $("#imagePreview").attr("src", data.image);
            },
            error: function () {
                console.error("加载失败");
            }
        });
    });

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

</script>

</body>
</html>
